import axios from 'axios'
import config from '@/env'
import { getToken } from '@/utils/auth'
import { ElNotification } from 'element-plus'

// 请求超时时间
const timeout = 120 * 1000
const host = config.api.host
let token = getToken() || '5c6e4f5a70bab851a601cd723dac08dfb5d948ac964f990af2099082534529c8'

// build http header
function buildHeader(option) {
  let headers = !token ? {} : { token: `${token}` }
  if (option) {
    headers = { ...headers, ...option }
  }
  return headers
}

/**
 * 错误数据处理卡口
 * @param {*} data
 * @returns
 */
function transformResponse(data) {
  let _data = JSON.parse(data)
  let { code } = _data
  if (code === 0) {
    return _data
  }
  if (!code === 0) {
    ElNotification({
      title: 'Success',
      message: 'This is a success message',
      type: 'success',
    })
    return _data
  }
}

// axios配置
let axiosConfig = (option = {}) => {
  return {
    baseURL: host,
    headers: buildHeader(option),
    timeout: timeout,
    transformResponse: [
      function (data) {
        return transformResponse(data)
      },
    ],
  }
}

let axiosCreater = (resquestConfig = {}) => {
  let nax = axios.create(resquestConfig)
  nax.interceptors.request.use(respConfig => {
    if (!token) {
      return respConfig
    }
    return respConfig
  })
  return nax
}

// http get method
export function get(url, data = {}, config = {}, curHost = host) {
  let nax = axiosCreater(axiosConfig({ ...config }))
  return nax
    .get(`${curHost}${url}`, {
      params: data,
    })
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}

// http post method
export function post(url, data = {}) {
  let nax = axiosCreater(
    axiosConfig({
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    })
  )
  return nax
    .post(`${host}${url}`, data)
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}

export function postJson(url, data = {}, config = {}, curHost = host) {
  let nax = axiosCreater(
    axiosConfig({
      responseType: 'json',
      'Content-Type': 'application/json;charset=utf-8',
      ...config,
    })
  )
  return nax
    .post(`${curHost}${url}`, data)
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}

export function postFile(url, data = {}) {
  let nax = axiosCreater({
    headers: buildHeader({}),
  })
  return nax
    .post(`${host}${url}`, data)
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}

export function postString(url, data = {}) {
  let nax = axiosCreater(
    axiosConfig({
      responseType: 'text',
      'Content-Type': 'application/json;charset=utf-8',
    })
  )
  return nax({
    url: `${host}${url}`,
    method: 'POST',
    data: data,
  })
    .then(res => {
      return res.data
    })
    .catch(error => {
      throw error
    })
}

// http post method
export function postForm(url, data = {}) {
  let nax = axiosCreater(axiosConfig())
  return nax
    .post(`${host}${url}`, data)
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}

// http delete method
export function del(url, data = {}) {
  let nax = axiosCreater(axiosConfig())
  return nax
    .delete(`${host}${url}`, {
      params: data,
    })
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}

export function postBlob(url, data = {}) {
  let nax = axiosCreater(axiosConfig())
  return nax
    .post(`${host}${url}`, data, { responseType: 'blob', timeout: 180000 })
    .then(res => {
      return res.data
    })
    .catch(err => {
      throw err
    })
}
